package com.ysd.web.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ysd.web.db.DBManager;
import com.ysd.web.db.Page;
import com.ysd.web.entity.News;
import com.ysd.web.entity.Product;

public class NewDaoimpl {
	
	public List<News> selectNews(){
		List<News> list=new ArrayList<News>();
		String sql="SELECT * from news";
		ResultSet rs=DBManager.executeQueryUpdate(sql);
		try {
			while(rs.next()){
				News n=new News();
				n.setNewid(rs.getInt(1));
				n.setNtid(rs.getInt(2));
				n.setTitle(rs.getString(3));
				n.setContent(rs.getString(5));
				n.setCreatetime(rs.getString(6));
				list.add(n);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<News> selectNewsType(int ntid){
		List<News> list=new ArrayList<News>();
		String sql="SELECT * FROM news where new_nid="+ntid+"";
		ResultSet rs=DBManager.executeQueryUpdate(sql);
		try {
			while(rs.next()){
				News n=new News();
				n.setNewid(rs.getInt(1));
				n.setNtid(rs.getInt(2));
				n.setTitle(rs.getString(3));
				n.setContent(rs.getString(5));
				n.setCreatetime(rs.getString(6));
				list.add(n);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public News selectNewsByid(int newid){
		Date time=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		News n=new News();
		String sql="SELECT * from news where newid="+newid+"";
		ResultSet rs=DBManager.executeQueryUpdate(sql);
		try {
			while(rs.next()){
				
				n.setNewid(rs.getInt(1));
				n.setNtid(rs.getInt(2));
				n.setTitle(rs.getString(3));
				n.setContent(rs.getString(5));
				n.setCreatetime(rs.getString(6));
			
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	public List<News> infotNews(News pro,Page page){
		if(pro.getTitle()==null){
			pro.setTitle("");
		}
		if(pro.getIscarefully()==null){
			pro.setIscarefully("");
		}
		List<News> list=new ArrayList<News>();
		String sql="SELECT * FROM news p LEFT JOIN news_type pt on p.new_nid=pt.nid where new_title like '%"+pro.getTitle()+"%' and iscarefully like '%"+pro.getIscarefully()+"%'LIMIT "+page.getPage()+","+page.getLimit()+"";
		ResultSet rs=DBManager.executeQueryUpdate(sql);
		try {
			while(rs.next()){
				News n=new News();
				n.setNewid(rs.getInt(1));
				n.setNtid(rs.getInt(2));
				n.setTitle(rs.getString(3));
				n.setContent(rs.getString(5));
				n.setCreatetime(rs.getString(6));
				n.setIscarefully(rs.getString(7));
				n.setNtype(rs.getString(9));
				
				list.add(n);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public int selectProTotal(News pro){
		List<Product> list=new ArrayList<Product>();
		Product p=new Product();
		if(pro.getTitle()==null){
			pro.setTitle("");
		}
		if(pro.getIscarefully()==null){
			pro.setIscarefully("");
		}
		String sql="SELECT count(*) FROM news p LEFT JOIN news_type pt on p.new_nid=pt.nid where new_title like '%"+pro.getTitle()+"%' and iscarefully like '%"+pro.getIscarefully()+"%' ";
		ResultSet rs=DBManager.executeQueryUpdate(sql);
		try {
			while(rs.next()){
				
				p.setTotalpage(rs.getInt(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p.getTotalpage();
	}
	public int addPro(News p){
		String sql="insert into news(new_title,new_content,new_nid,iscarefully) VALUES('"+p.getTitle()+"','"+p.getContent()+"',"+p.getNtid()+",'"+false+"')";
		int r=DBManager.executeUpdate(sql);
		System.out.println(sql);
		return r;
}
	public int updatePro(News up){
		String sql="UPDATE news set new_title='"+up.getTitle()+"',new_content='"+up.getContent()+"',new_nid="+up.getNtid()+" where newid="+up.getNewid()+"";
		int r=DBManager.executeUpdate(sql);
		return r;
	}
	public int delPro(int newid){
		String sql="delete from news where newid="+newid+"";
		int r=DBManager.executeUpdate(sql);
		return r;
	}
}
